// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        l1: cc.Node, //左侧背景1
		l2: cc.Node, //左侧背景2
		r1: cc.Node, //右侧背景1
		r2: cc.Node, //右侧背景2
		speed: 50, // 背景移动速度
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
		this.node.on(cc.Node.EventType.TOUCH_START,this.onTouchStart,this)
		// 获取重置触发坐标点
		this.triggerLY = -this.l1.height;
		this.triggerRY = -this.r1.height;
	},
	onTouchStart (event) {
		console.log('点击屏幕');
		this.bgMove();
	},
	bgMove () {
		// 背景移动
		this.l1.y -= this.speed;
		this.l2.y -= this.speed;
		
		this.r1.y -= this.speed;
		this.r2.y -= this.speed;
 
		// 重置
		if (this.l1.y <= this.triggerLY) 
			this.l1.y = this.l2.y+this.l1.height;
		else if (this.l2.y <= this.triggerLY) 
			this.l2.y = this.l1.y+this.l1.height;
		
		if (this.r1.y <= this.triggerRY) 
			this.r1.y = this.r2.y+this.r1.height;
		else if (this.r2.y <= this.triggerRY) 
			this.r2.y = this.r1.y+this.r1.height;
	},
    start () {

    },

    // update (dt) {},
});
